import { fetchInfo } from "@/api/account";
import router from "@/router";
const state = {
  info: {
    user: {},
    menus: []
  },
  token: ""
};

const getters = {
  getInfo: state => {
    return state.info;
  },
  getToken: state => {
    return state.token;
  }
};

const actions = {
  pullInfo: ({ commit }) => {
    fetchInfo().then(res => {
      if (res.data.code === "200") {
        commit("setInfo", res.data.data);
      }
      if (res.data.code === "401") {
        router.replace("login");
      }
    });
  },
  clearInfo: ({ commit }) => {
    commit("setInfo", {
      user: {},
      menus: []
    });
  }
};

const mutations = {
  setInfo: (state, info) => {
    state.info = info;
  }
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
};
